package main

import (
	"log"

	"go-emacs-module"
)

// emacs  -l atest.el -batch

var testit = true

func test_0() {
	if testit == false {
		return
	}

	test_2()
	// test_1()
}

// add a goem-print function
func test_3() {
	env := em.GetEnv()
	env.NonLocalExitClear()

	v7 := env.MakeInt(777)
	log.Println(v7)
	fh := goem.MkFunc(func(args []goem.Value, ud interface{}) goem.Value {
		log.Println(args, ud, "====")
		rv := env.MakeInt(888)
		log.Println(rv)
		return rv
	}, "it's goem-print function", 567)
	log.Println(fh, env.NonLocalExitCheck())
	// r := env.Funcall(fh, nil)
	// log.Println(r, env.NonLocalExitCheck())
	goem.Fset0("goem-print", fh)
	log.Println(env.NonLocalExitCheck())
}

// fset var
func test_2() {
	env := em.GetEnv()
	env.NonLocalExitClear()

	v := goem.MkInt(8484847345)
	goem.Set0("goem-vari", v)
	goem.Set0("goem-vars", goem.MkStr("hehheestr"))
	goem.Set0("goem-varf", goem.MkFloat(1.23456))

	log.Println(env.NonLocalExitCheck())
}

// basic
func test_1() {

	env := em.GetEnv()
	log.Println(env)
	v1 := env.MakeFloat(1.2345)
	v2 := env.MakeString("hehehee")
	log.Println(v1, v2, v2.ToString(), v1.ToFloat())

	{
		// v := C.em_intern_forgo(env.env, C.CString("fset"))
		// log.Println(v)
		// C.em_test_intern(env.env, C.CString("fset"))

	}

	{
		fv := goem.MkFunc(func(args []goem.Value, ud interface{}) goem.Value {
			log.Println(ud, "<-ud")
			return goem.MkInt(1234567)
		}, "goemf1", 123)
		log.Println(fv)

		fr := env.Funcall(fv, nil)

		log.Println(fr, fr.ToInt(), fr.Type())
		log.Println(fr.Type().IsNotNil())

		log.Println(env.NonLocalExitCheck())
		// why after one of these 3 line, ithe Intern function faild?
		// oh, because there is error.
		log.Println(fr.Type().ToFloat())
		log.Println(fr.Type().ToInt())
		log.Println(fr.Type().ToString())
		log.Println(env.NonLocalExitCheck())
		ety, signal, throw := env.NonLocalExitGet()
		log.Println(ety, signal, throw)
		log.Println(ety, signal.IsNotNil(), throw.IsNotNil())
		env.NonLocalExitClear()
		printfn := env.Intern("prin1-to-string")
		prv := env.Funcall(printfn, []goem.Value{signal})
		log.Println(prv, prv.ToString(), printfn)
		log.Println(env.NonLocalExitCheck())
		log.Println(ety, signal.ToString(), throw.ToString())
	}

	{
		// env.NonLocalExitClear()
		log.Printf("%p\n", env)
		close_all_buffers := env.Intern("provide")
		fr := env.Funcall(close_all_buffers, nil)
		log.Println(fr, close_all_buffers, close_all_buffers.IsNotNil())
	}

}
